#include "task_command.h"

namespace base::tasking {

int TaskCommandComparator::priorityWeight(TaskPriority priority)
{
    switch (priority) {
    case TaskPriority::Critical:
        return 3;
    case TaskPriority::High:
        return 2;
    case TaskPriority::Normal:
        return 1;
    case TaskPriority::Low:
    default:
        return 0;
    }
}

bool TaskCommandComparator::operator()(const TaskCommand::Ptr& lhs, const TaskCommand::Ptr& rhs) const
{
    if (!lhs || !rhs) {
        return false;
    }

    const int leftWeight = priorityWeight(lhs->priority);
    const int rightWeight = priorityWeight(rhs->priority);

    if (leftWeight == rightWeight) {
        // 先进先出
        return lhs->enqueuedAt > rhs->enqueuedAt;
    }

    // priority_queue 默认将 "最大" 元素放在顶部，因此这里返回 true 表示 lhs 的优先级更低
    return leftWeight < rightWeight;
}

} // namespace base::tasking
